/*
 * Copyright (c) 2010-2011 GOV.
 * [Id:UserService.java  11-8-20 下午4:51 poplar.mumu ]
 */
package org.mumu.pawn.cmps.def;

import org.jboss.logging.Logger;
import org.jboss.seam.faces.context.conversation.Begin;
import org.jboss.seam.faces.context.conversation.End;
import org.joda.time.DateTimeUtils;
import org.mumu.pawn.beans.common.AccountSessionBean;
import org.mumu.pawn.cmps.UserCmp;
import org.mumu.pawn.constants.Constants;
import org.mumu.pawn.datamodal.EntityDataModalFactory;
import org.mumu.pawn.domain.User;
import org.mumu.pawn.fm.dm.LazyPawnDataModel;
import org.mumu.pawn.util.MD5Util;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 * 默认用户组件.
 * <br/>
 *
 * @author poplar.mumu
 * @version 1.0 11-8-20 下午4:51
 * @since JDK 1.5
 */
@SuppressWarnings({"CdiInjectionPointsInspection"})
public class DefUserCmp implements UserCmp {
    private static final long serialVersionUID = 6739393833897133376L;

    @Inject
    private EntityManager entityManager;

    @Inject
    private Logger log;

    @Inject
    private AccountSessionBean accountSessionBean;

    @Begin
    @End
    public void saveOrUpdateUser(User arg_user) {
        //如果ID不为空，表示编辑一个用户信息
        if (arg_user.getId() != 0) {
            log.debug("更新用户信息:" + arg_user.getUserName());
            User existUser = getUser(arg_user.getId());
            existUser.setBirthday(arg_user.getBirthday());
            existUser.setUserName(arg_user.getUserName());
            existUser.setNickName(arg_user.getNickName());
            existUser.setGender(arg_user.getGender());
            existUser.setHomeAddress(arg_user.getHomeAddress());
            existUser.setHomePhone(arg_user.getHomePhone());
            existUser.setEmail(arg_user.getEmail());
            existUser.setMobile(arg_user.getMobile());
            existUser.setQq(arg_user.getQq());
            existUser.setUpdateActor(accountSessionBean.getAccount().getId());
            existUser.setUpdateTime(DateTimeUtils.currentTimeMillis());
            entityManager.persist(existUser);
            return;
        }
        log.debug("新增用户信息:" + arg_user.getUserName());
        arg_user.setPassword(MD5Util.getInstance().getMD5ofStr(arg_user.getPlaintextPassword()));
        arg_user.setState(Constants.USER_STATE_USE);
        entityManager.persist(arg_user);
    }

    /**
     * 构建用户数据提供模型。
     *
     * @return 用户数据提供模型
     */
    public LazyPawnDataModel createUserDataModal() {
        return EntityDataModalFactory.builderDataModal(entityManager, User.class);
    }


    /**
     * 获取某个用户信息.
     *
     * @param arg_id 用户ID
     * @return 用户信息
     */
    public User getUser(long arg_id) {
        return entityManager.find(User.class, arg_id);
    }

    /**
     * 根据用户名称查询用户信息。如果用户不存在返回null
     *
     * @param arg_userName 用户名称
     * @return 用户信息
     */
    public User findUser(String arg_userName) {
        Query query = entityManager.createQuery("from  User u where u.userName =:name");
        query.setParameter("name", arg_userName);
        query.setMaxResults(2);
        List<User> userList = query.getResultList();
        return userList.size() == 0 ? null : userList.get(0);
    }

    @Override
    public void delteUser(User arg_user) {
        User existUser = getUser(arg_user.getId());
        if (existUser != null) {
            existUser.setState(Constants.USER_STATE_DELETE);
            updateUser(existUser);
            return;
        }
        log.info("无法找到用户");
    }

    @Override
    public void disabledUser(User arg_user) {
        User existUser = getUser(arg_user.getId());
        if (existUser == null) {
            log.info("无法找到用户");
            return;
        }
        int userState = existUser.getState();
        existUser.setState((userState == Constants.USER_STATE_DISABLED) ? Constants.USER_STATE_USE :
                (userState == Constants.USER_STATE_USE ? Constants.USER_STATE_DISABLED : userState));
        updateUser(existUser);

    }

    private void updateUser(User arg_user) {
        arg_user.setUpdateActor(accountSessionBean.getAccount().getId());
        arg_user.setUpdateTime(DateTimeUtils.currentTimeMillis());
        entityManager.persist(arg_user);
    }
}
